package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseSinglePaneActivity;
import com.a0soft.gphone.aTruffleHog.pref.Pref;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class AgreementWnd extends BaseSinglePaneActivity {
    private boolean m_bShowStatWndAfterAgree; 
    
    private static final String EXTRA_SHOW_STAT_WND = "show_stats";
    
    private static final String LOG_TAG = AgreementWnd.class.getSimpleName();

    ////////////////////////////////////////////
    @Override protected Fragment 
    onCreatePane() 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreatePane()");
        
        m_bShowStatWndAfterAgree = true;
        
        Intent it = getIntent();
        if (it != null)
            m_bShowStatWndAfterAgree = it.getBooleanExtra(EXTRA_SHOW_STAT_WND, m_bShowStatWndAfterAgree);
        
        return new AgreementFrg();
    }

    @Override protected void 
    onPostCreate(Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onPostCreate()");
        super.onPostCreate(bdSavedInstanceState);
        
        getActivityHelper().setupSubActivity();
    }
    
    
    @Override public void 
    onNotifyByFragment(Class<?> clsFrg, Intent it)
    {
        if (aDebug.ENABLED) {
            aDebug.d(LOG_TAG, "onNotifyByFragment()");
        }
        if (it == null || !clsFrg.equals(AgreementFrg.class)) return;
        
        int nBtn = it.getIntExtra(AgreementFrg.EXTRA_BTN, 
                                  AgreementFrg.BTN_NONE);
        switch (nBtn) {
        case AgreementFrg.BTN_AGREE:
            Pref.SetAgreeAgreement(this, true);
            finish();
            if (m_bShowStatWndAfterAgree)
                DashboardFrg.ShowStatsWnd(this);
            break;
        case AgreementFrg.BTN_DISAGREE:
            finish();
            break;
        }
    }

    ////////////////////////////////////////////
    public static class AgreementFrg extends BaseFragment {
        private static final String EXTRA_PREFIX = AgreementFrg.class.getName();
        private static final String EXTRA_BTN = EXTRA_PREFIX + ".btn";
        private static final int BTN_AGREE = 100;
        private static final int BTN_DISAGREE = 101;
        private static final int BTN_NONE = 0;
        
        @Override public View 
        onCreateView(LayoutInflater inflater, ViewGroup vgContainer,
                     Bundle bdSavedInstanceState) 
        {
            if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreateView()");
            Context ctx = inflater.getContext();
            
            View vRoot = inflater.inflate(R.layout.agreement_frg, vgContainer, false);
            
            WebView wv = (WebView)vRoot.findViewById(R.id.content);
            wv.loadUrl(AboutWnd.GetAssertRootPath(inflater.getContext()) + 
                       "send_data.htm");
            
            boolean bShowBtns = Pref.IsAgreeAgreement(ctx) ? false : true;            
            View v;
            v = vRoot.findViewById(R.id.agree);
            if (bShowBtns) {
                v.setOnClickListener(new View.OnClickListener() {            
                    @Override public void 
                    onClick(View v)
                    {
                        Intent it = new Intent();
                        it.putExtra(EXTRA_BTN, BTN_AGREE);
                        GetBaseActivity().notifyByFragment(AgreementFrg.this, it);
                    }
                });
            }
            else {
                v.setEnabled(false);
            }
            
            v = vRoot.findViewById(R.id.disagree);
            if (bShowBtns) {
                v.setOnClickListener(new View.OnClickListener() {            
                    @Override public void 
                    onClick(View v)
                    {
                        Intent it = new Intent();
                        it.putExtra(EXTRA_BTN, BTN_DISAGREE);
                        GetBaseActivity().notifyByFragment(AgreementFrg.this, it);
                    }
                });
            }
            else {
                v.setEnabled(false);
            }
            
            vRoot.findViewById(R.id.preview).setOnClickListener(new View.OnClickListener() {            
                @Override public void 
                onClick(View v)
                {
                    DashboardFrg.ShowDeviceInfoWnd(getActivity(), true);
                }
            });
            
            return vRoot;
        }
    }
}
